/* eslint-disable prettier/prettier */
import { contextBridge } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'
import {
  loading,
  openBrowser,
  errorMsg,
  errorMsg2,
  successMsg,
  successMsg2,
  warnMsg,
  defaultLng,
  winOpt,
  switchLng,
  getLng,
  setAutoLaunch,
  isAutoLaunch
} from './scripts/common'
import {
  conicColors,
  progressSize,
  items,
  setActive,
  active,
  activeShadow,
  inactive,
  setTopData
} from './scripts/top'
import config from './scripts/config'
import { netInfo } from './scripts/network'
import SysUtil from './scripts/utils/SysUtil'
import { cpuInfo, cpuLoadInfo, contentStyle, setHt, getCpuOption } from './scripts/cpu'
import { batteryInfo, setBatteryHt } from './scripts/battery'
import { allDisks, setDiskHt } from './scripts/disk'
import { memInfo, topInfo, setMemHt, getOption, getMemInf, setMem } from './scripts/mem'
import { getData } from './scripts/data'
import { setDatas } from './scripts/bottom'
import { listener } from './scripts/title'

// Custom APIs for renderer
const api = {
  common: {
    loading,
    openBrowser,
    errorMsg,
    errorMsg2,
    successMsg,
    successMsg2,
    warnMsg,
    defaultLng,
    winOpt,
    switchLng,
    getLng,
    setAutoLaunch,
    isAutoLaunch
  },
  top: { conicColors, progressSize, items, setActive, active, activeShadow, inactive, setTopData },
  config,
  netInfo,
  sysUtil: SysUtil,
  cpu: { cpuInfo, cpuLoadInfo, contentStyle, setHt, getCpuOption },
  battery: { batteryInfo, setBatteryHt },
  disk: { allDisks, setDiskHt },
  mem: { memInfo, topInfo, setMemHt, getOption, getMemInf, setMem },
  data: { getData },
  bottom: { setDatas },
  title: { listener }
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = electronAPI
  window.api = api
  window.topInfo = [0,0,0,0]
}
